package com.xxh.dataaccess.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxh.dataaccess.dao.CommentDao;
import com.xxh.dataaccess.dto.CommentDto;
import com.xxh.dataaccess.entity.Comment;
import com.xxh.dataaccess.service.CommentService;
import com.xxh.dataaccess.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (Comment)表服务实现类
 *
 * @author makejava
 * @since 2021-03-11 15:16:02
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentDao, Comment> implements CommentService {


    @Autowired
    UserService userService;


    @Override
    public List<CommentDto> getComments(String bv) {

        //获取父评论
        List<CommentDto> comments = baseMapper.getCommentDtoByBv(bv);
        //获取顶级评论的子评论
        comments.stream().forEach(i -> {
            if (i.getParent_id() == 0) {
                List<CommentDto> child = baseMapper.getCommentDtoByParentID(i.getId());
                if (child != null) {
                    i.setChildrenList(child);
                }
            }
        });

        List<CommentDto> collect = comments.stream().filter((i) -> {
            return i.getParent_id() == 0;
        }).collect(Collectors.toList());
        return collect;
    }


    @Override
    public int insert(Comment comment) {
        return baseMapper.insert(comment);

    }


}
